<template>
  <div>
    <basic-container>
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>内容管理</el-breadcrumb-item>
        <el-breadcrumb-item> 动态管理 </el-breadcrumb-item>
      </el-breadcrumb>
    </basic-container>
    <basic-container>
      <avue-crud
        ref="crud"
        :data="data"
        v-model="form"
        :option="option"
        :page.sync="page"
        @on-load="getList"
        @refresh-change="refreshChange"
        @search-reset="form = {}"
        @search-change="searchChange"
        @row-update="rowUpdate"
        @row-del="rowDel"
        @row-save="rowSave"
      >
        <template slot="last" slot-scope="scope">
          {{ scope.row.update_time }}
          <el-tag>{{ scope.row.edit_account_name }}</el-tag>
        </template>
        <template slot-scope="scope" slot="menu">
          <el-button
            v-if="scope.row.is_top == undefined || scope.row.is_top == 0"
            icon="el-icon-edit"
            type="text"
            size="small"
            @click.stop="isTop(scope.row, true)"
            >置顶</el-button
          >
          <el-button
            v-if="scope.row.is_top == 1"
            icon="el-icon-edit"
            type="text"
            size="small"
            @click.stop="isTop(scope.row, false)"
            >取消置顶
          </el-button>
        </template>
      </avue-crud>
    </basic-container>
  </div>
</template>

<script>
import { common } from "@/api/cat/httpRequest";
import { catUrl } from "@/config/env";
import { catDynamic as baseURL, uploadFile } from "@/api/cat/baseUrls";
export default {
  data() {
    return {
      page: {
        total: 0,
        pageSize: 10,
      },
      type: true,
      data: [],
      params: {},
      form: {},
      default: {
        orderByList: "create_time&DESC",
      },
      option: {
        addBtn: true,
        editBtn: true,
        viewBtn: false,
        delBtn: true,
        excelBtn: true,
        selection: true,
        border: true,
        column: [
          {
            label: "用户id",
            prop: "user_id",
            type: "input",
            search: true,
            placeholder: "用户id或马甲id",
            // rules: [
            //   {
            //     required: true,
            //     message: "用户id或马甲id必须填写",
            //   },
            // ],
          },
          {
            label: "用户名称",
            prop: "author_name",
            type: "input",
            search: false,
            addDisplay: false,
            editDisplay: false,
            addDisabled: true,
            editDisabled: true,
          },
          {
            label: "动态内容",
            prop: "dynamic_text",
            type: "textarea",
            span: 13,
            rules: [
              {
                required: true,
                message: "动态内容必须填写",
              },
            ],
          },
          {
            type: "upload",
            label: "动态图片",
            span: 24,
            display: true,
            showFileList: true,
            propsHttp: {},
            canvasOption: {},
            prop: "picture_url",
            dataType: "string",
            listType: "picture-card",
            action: `${catUrl}${uploadFile.uploadFile}`,
            fileSize: 10240,
            limit: 3,
            propsHttp: {
              url: "source",
            },
            // rules: [
            //   {
            //     required: true,
            //     message: "动态图片必须上传",
            //   },
            // ],
          },
          {
            label: "关联书籍id",
            prop: "book_id",
            type: "input",
            search: true,
          },
          {
            label: "关联书籍名称",
            prop: "book_title",
            type: "input",
            search: true,
            addDisplay: false,
            editDisplay: false,
            addDisabled: true,
            editDisabled: true,
          },
          {
            label: "是否置顶",
            prop: "is_top",
            type: "select",
            hide: true,
            search: true,
            dicData: [
              {
                label: "是",
                value: 1,
              },
              {
                label: "否",
                value: 0,
              },
            ],
            rules: [
              {
                required: true,
                message: "是否置顶必须填写",
              },
            ],
          },
          {
            label: "最后操作信息",
            prop: "last",
            type: "input",
            slot: true,
            addDisplay: false,
            editDisplay: false,
            addDisabled: true,
            editDisabled: true,
          },
        ],
      },
    };
  },
  methods: {
    /**是否置顶 */
    async isTop(dynamic, state) {
      if (state) {
        dynamic.is_top = 1;
      } else {
        dynamic.is_top = 0;
      }
      await common(baseURL.isTop, { ...dynamic }, null, "POST");
      this.getList();
    },
    /**获取列表 */
    async getList() {
      this.loading = true;
      //清空表格列表
      this.data = [];
      //发送请求获取数据
      let response = await common(
        baseURL.pageAndParam,
        { ...this.params, ...this.default },
        this.page,
        "POST"
      );
      this.page = response.page;
      this.data = response.data;
      this.loading = false;
    },
    /**保存 */
    async rowSave(row, done, loading) {
      delete row["id"];
      row.create_time = new Date();
      let userInfo = this.$store.state.user.userInfo;
      //       scope.row.admin_id = userInfo.id ? userInfo.id : null
      //       scope.row.admin_name = userInfo.username
      row.edit_account_name = userInfo.username;
      row.update_time = new Date();
      let data = await common(baseURL.save, row, null, "POST");
      if (data == null) {
        loading();
        return;
      }
      this.getList();
      done();
    },
    /**修改 */
    async rowUpdate(row, index, done, loading) {
      if (row.user_id.length > 32) {
        loading();
        return this.$message.error("用户或马甲id错误");
      }
      let userInfo = this.$store.state.user.userInfo;
      row.edit_account_name = userInfo.username;
      row.update_time = new Date();
      let data = await common(baseURL.update, row, null, "POST");
      if (data == null) {
        loading();
        return;
      }
      this.getList();
      done();
    },
    /**刷新 */
    refreshChange() {
      this.getList();
    },
    /**删除 */
    rowDel(row) {
      this.$confirm("此操作将永久删除, 是否继续?", "提示", {
        type: "warning",
      })
        .then(async () => {
          let response = await common(
            baseURL.delete,
            { id: row.dynamic_id },
            null,
            "GET"
          );
          // if (response == null) {
          //   return this.$message.error("操作失败");
          // }
        })
        .then(() => {
          this.getList();
        });
    },
    /**搜索 */
    searchChange(params, done) {
      this.params = params;
      this.page.currentPage = 1;
      this.getList();
      if (done) done();
    },
  },
};
</script>